totems
A collection of lightweight assertion macros for unit testing in Rust.
I started this project solely to get more practice writing macros in Rust. Other than perhaps nicer error messages, there isn't anything in here that cannot be done with one or more regular assert!()
macros.
That being said, I do particularly like (and actually use) the assert_ok!()
, assert_err!()
, and assert_some!()
macros with comparisons to both check the state of the enum and the inner value on the same line, which is why this exists as a crate.
Enums
assert_ok!()
Syntax:
use assert_ok;
let result = "5".;
assert_ok!;
Equivalent to:
assert!;
assert_ok!() with comparisons
Syntax:
use assert_ok;
let result = "5".;
assert_ok!;
assert_ok!;
assert_ok!;
assert_ok!;
assert_ok!;
assert_ok!;
Equivalent to:
assert!
assert!;
assert_err!()
Synax:
use assert_err;
let result = "z".;
assert_err!;
Equivalent to:
assert!;
assert_err!() with comparisons
Synax:
use assert_err;
let result: = Err;
assert_err!;
assert_err!;
assert_err!;
assert_err!;
assert_err!;
assert_err!;
Equivalent to:
assert!;
assert!;
assert_some!()
Synax:
use assert_some;
let option = "5"..ok;
assert_some!;
Equivalent to:
assert!;
assert_some!() with comparisons
Synax:
use assert_some;
let option = "5"..ok;
assert_some!;
assert_some!;
assert_some!;
assert_some!;
assert_some!;
assert_some!;
Equivalent to:
assert!;
assert!;
assert_none!()
Synax:
use assert_none;
let option = "z"..ok;
assert_none!;
Equivalent to:
assert!;
Tuples
assert_(0 to 15)th
Synax:
use // pseudocode
let tuple = ;
let x = 1;
assert_0th!; // tuple.0 >= x
assert_1st!;
assert_2nd!;
assert_3rd!;
assert_4th!;
assert_5th!;
assert_6th!;
assert_7th!;
assert_8th!;
assert_9th!;
assert_10th!;
assert_11th!;
assert_12th!;
assert_13th!;
assert_14th!;
assert_15th!;
Equivalent to:
assert!;
...
assert!;
Collections
assert_contains!()
Synax:
use assert_contains;
let vec = vec!;
let x = 5;
assert_contains!;
Equivalent to:
assert!;
assert_all!()
Syntax:
use assert_all;
let vec = vec!;
assert_all!;
Equivalent to:
assert!;
assert_any!()
Syntax:
use assert_any;
let vec = vec!;
assert_any!;
Equivalent to:
assert!;
assert_nth!()
Syntax:
use assert_nth;
let vec = vec!;
let x = 5;
assert_nth!; // vec[2] == x
assert_nth!;
assert_nth!;
assert_nth!;
assert_nth!;
Equivalent to:
assert!;
Inequalities
Syntax:
use ;
assert_lt! // less than
assert_le! // less or equal
assert_gt! // greater than
assert_ge! // greater or equal
Equivalent to:
assert!;
assert!;
assert!;
assert!;